package com.gxa.www.cl.service.impl;

import com.gxa.www.cl.common.util.DCMd5Util;
import com.gxa.www.cl.domain.entity.Admin;
import com.gxa.www.cl.domain.entity.Employee;
import com.gxa.www.cl.mapper.AdminMapper;
import com.gxa.www.cl.mapper.EmployeeMapper;
import com.gxa.www.cl.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public boolean save(Employee employee) {
        int userId = createAdminForEmployee(employee); // 获取 user_id
        employee.setUser_id(userId); // 设置 user_id 到员工对象
        return employeeMapper.insert(employee) > 0;
    }

    @Override
    public List<Employee> getEmployees() {
        return employeeMapper.selectAll();
    }

    @Override
    public List<Employee> getAllEmployees() {
        return employeeMapper.selectAll();
    }

    @Override
    public boolean deleteEmployee(int id) {
        int result = employeeMapper.deleteById(id);
        return result > 0;  // 返回删除是否成功
    }

    @Override
    public int createAdminForEmployee(Employee employee) {
        Admin admin = new Admin();
        admin.setUser_name(employee.getName()); // 假设员工的名字就是管理员的名字
        String pwd = DCMd5Util.getMd5("123456");
        admin.setUser_pwd(pwd); // 默认密码
        adminMapper.insert(admin);
        return admin.getUser_id(); // 返回插入后的 user_id
    }
}
